﻿package com.petermacko.effects.pixelwarp{
	import flash.events.*;
	import flash.display.*;
	import flash.filters.*;
	import fl.transitions.*;
	import fl.transitions.easing.*;
	import flash.geom.Matrix;
	import flash.utils.Timer;

	public class digitalWarpIn extends MovieClip
	{
		public var fIS:String;
		public var mLM:MovieClip;
		private var fT:Timer;
		private var fPX:Number, fPY:Number, fPW:Number, fPH:Number;
		private var fGF:GlowFilter;
		private var fB:BlurFilter;
		private var fHI:MovieClip; //This is the public variable that will accept the library Class ID
		private var fLIP:DisplayObjectContainer;
		private var fPC:uint;
		
		//Define what the library obfPCect is
		private var fLI:MovieClip; //Make this dynamic for class == myLibrary Movie
		private var fPM:Matrix; //Matrix for Generating Fragments
		private var fP:MovieClip; //Define the new pixel clip
		private var fPBD:BitmapData;
		private var fPB:Bitmap;
		private var fTw:Tween;
		private var tR:MovieClip;
		private var tPVX:Number, tPVY:Number, sec:Number;

		public function warpIn(mLM:MovieClip, tPVX:Number, tPVY:Number, seconds:Number):void {
			sec = seconds;
			fPX = tPVX;
			fPY = tPVY;
			fT = new Timer(200, 0);
			fT.addEventListener(TimerEvent.TIMER, fnxWarpObj);
			fLI = new MovieClip();
			fLI = mLM;
			fGF = new GlowFilter(0xffffff,0.75,7,5,1,1,true,false);
			fB = new BlurFilter(15,15,0.75);
			fHI = new MovieClip();
			fLIP = new MovieClip();
			fPC = 0;
			fPW = 0;
			fPH = 0;
			fLI = mLM;
			fLIP = this.parent;
			fLIP.addChild(fLI);
			fnxExecute();
		}
		private function fnxExecute():void {
			fPW = fLI.width / 10;
			fPH = fLI.height / 10;
			for (fPC = 0; fPC < 100; fPC++) {
				fPM = new Matrix(1,0,0,1,0,0);
				fP = new MovieClip();
				fPBD = new BitmapData(((Math.random() * fPW) + 10), ((Math.random() * fPH) + 10), true, 0xFFFFFF);
				fPB = new Bitmap(fPBD);
				fP.alpha = 0;
				if (fPC < 10) {
					fPM.tx -= (fPC * fPW);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = fPC * fPW;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 11 && fPC < 22) {
					fPM.tx -= ((fPC - 11) * fPW);
					fPM.ty = -(fPH);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 11) * fPW);
					fP.y = fPH;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 22 && fPC < 33) {
					fPM.tx -= ((fPC - 22) * fPW);
					fPM.ty = -(fPH * 2);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 22) * fPW);
					fP.y = fPH * 2;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 33 && fPC < 44) {
					fPM.tx -= ((fPC - 33) * fPW);
					fPM.ty = -(fPH * 3);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 33) * fPW);
					fP.y = fPH * 3;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 44 && fPC < 55) {
					fPM.tx -= ((fPC - 44) * fPW);
					fPM.ty = -(fPH * 4);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 44) * fPW);
					fP.y = fPH * 4;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 55 && fPC < 66) {
					fPM.tx -= ((fPC - 55) * fPW);
					fPM.ty = -(fPH * 5);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 55) * fPW);
					fP.y = fPH * 5;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 66 && fPC < 77) {
					fPM.tx -= ((fPC - 66) * fPW);
					fPM.ty = -(fPH * 6);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 66) * fPW);
					fP.y = fPH * 6;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 77 && fPC < 88) {
					fPM.tx -= ((fPC - 77) * fPW);
					fPM.ty = -(fPH * 7);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 77) * fPW);
					fP.y = fPH * 7;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC >= 88 && fPC < 99) {
					fPM.tx -= ((fPC - 88) * fPW);
					fPM.ty = -(fPH * 8);
					fPBD.draw(fLI,fPM,null,null,null,false);
					fPB.bitmapData = fPBD;
					fP.x = ((fPC - 88) * fPW);
					fP.y = fPH * 8;
					fP.addEventListener(Event.ENTER_FRAME, moveSprites);
					fP.addChild(fPB);
					fHI.addChild(fP);
				}
				if (fPC + 1 == 100) {
					fHI.addEventListener(Event.ENTER_FRAME, fnxTrackPieces);
					fHI.x = fPX;
					fHI.y = fPY;
					fLI.x = fPX;
					fLI.y = fPY;
					fLI.alpha = 0;
					fLIP.addChild(fHI);
					fLIP.addChild(fLI);
					fT.start();
				}
			}
		}
		private function moveSprites(e:Event):void {
			fGF.strength = 2;
			fGF.blurX -= .05;
			fGF.blurY -= .05;
			fB.blurX -= .005;
			fB.blurY -= .005;
			if (e.target.alpha >= 2) {
				tR = new MovieClip();
				tR = e.target as MovieClip;
				if (tR.parent) {
					tR.parent.removeChild(tR);//null
					e.target.removeEventListener(Event.ENTER_FRAME, moveSprites);
				}
			} else {
				e.target.alpha += Math.random() * .2 + .001;
			}
			if (fLI.alpha == 1) {
				null;
			}
			e.target.filters = [fGF, fB];
		}
		private function fnxWarpObj(e:TimerEvent):void {
			fTw = new Tween(fLI, "alpha", Strong.easeOut, 0, 1, sec, true);
			fT.removeEventListener(TimerEvent.TIMER, fnxWarpObj);
		}
		private function fnxTrackPieces(e:Event):void {
			fHI.x = fLI.x;
			fHI.y = fLI.y;
			if (fHI.numChildren == 0) {
				fHI.removeEventListener(Event.ENTER_FRAME, fnxTrackPieces);
				fLIP.removeChild(fHI);
			}
		}
	}
}